<template>
  <el-dialog :close-on-click-modal="false" :title="form.id ? '编辑' : '新增'" :visible="show" :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="班组" prop="teamName">
        <el-input disabled v-model="form.teamName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="日期" prop="days">
        <el-input type="textarea" rows="3" disabled v-model="form.day" placeholder="请输入"></el-input>
      </el-form-item>
    <el-form-item label="时段类型" prop="timeSegment">
        <el-radio-group v-model="form.segmentType" @change="onChangeType">
            <el-radio :label="0">临时</el-radio>
            <el-radio :label="1">固定</el-radio>
        </el-radio-group>

        <SelectTimeRanges v-if="form.segmentType == 0" v-model="form.timeSegment" @input="workTimeInfoChange"></SelectTimeRanges>
        <SelectWorkTime v-if="form.segmentType == 1" v-model="form.timeSegment" @input="workTimeInfoChange"></SelectWorkTime>
      </el-form-item>
      <!-- <el-form-item label="时段" prop="timeSegment">
        <selectTimeRanges v-model="form.timeSegment" @input="workTimeInfoChange"></selectTimeRanges>
      </el-form-item> -->
      <el-form-item label="时长" prop="hourTime">
        <!-- <el-input disabled style="width: 350px" v-model="form.hourTime" placeholder="请输入"></el-input> -->
        {{ form.timeHour ? parseSeconds(form.timeHour) : '0分' }}
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-color-picker v-model="form.color"></el-color-picker>
      </el-form-item>
      <el-form-item label="选择路线" prop="route">
        <SelectByApis v-model="form.route" :api="roadList" :filterable="true"></SelectByApis>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import SelectByApis from '@/components/SelectbyApis';
import {parseSeconds} from '@/utils'
import { workEdit } from '@/api/workShift';
import { roadList } from "@/api/checkRoad";
// 选择时间范围多个
import SelectTimeRanges from './selectTimeRanges.vue';
import SelectWorkTime from './selectWorkTime.vue'

export default {
  components: {
    SelectTimeRanges,
    SelectWorkTime,
    SelectByApis
  },
  props: {
    /* 显示隐藏 */
    show: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => {
        return {};
      }
    },
    clazz: {
        type: String,
        default: ''
    },
    teamId: {
        type: [Number, null],
        default: null
    },
    teamName: {
        type: [String, null],
        default: null
    }
  },
  watch: {
    item: {
      handler(val) {
        this.form = val || {};
      },
      immediate: true
    },
    show: {
      handler(val) {
        val && this.resetForm('form');
      },
      immediate: true
    }
  },
  data() {
    return {
      form: {},
      rules: {
        // code: [
        //   {
        //     required: true,
        //     message: '请输入内容',
        //     trigger: ['change', 'input']
        //   }
        // ]
      }
    };
  },
  methods: {
    parseSeconds,
    roadList,
    // 处理时段计算时长
    workTimeInfoChange() {
      if (this.form.timeSegment) {
        const list = JSON.parse(this.form.timeSegment);
        let second = 0;
        list.forEach((item) => {
          if (item.endTime && item.startTime) {
            let endTemp = +new Date(this.form.workDay + ' ' + item.endTime)
            let startTemp = +new Date(this.form.workDay + ' ' + item.startTime)
            second += endTemp - startTemp;
          }
        });
        // this.form.hourTime = this.formatSeconds(second / 1000);
        this.form.timeHour = parseInt(second / 1000);
      }
    },
    submit() {
      console.log('打印当前编辑：', this.form);
      this.$refs['form'].validate((valid) => {
        if (valid) {
            let params = {...this.form}
            if(this.clazz == 'member') {
                params = Object.assign({}, this.form, {teamId: this.teamId, teamName: this.teamName})
            }

            if(!params.memberId) {
                delete params.memberId;
                delete params.memberName;
            }
          if (this.form.day) {
            workEdit(params).then((res) => {
              // 验证成功-编辑
              this.close();
            });
          }
        }
      });
    },
    close() {
      this.form = {};
      this.$emit('change');
      this.$emit('update:show', false);
    },
    onChangeType() {
        this.$set(this.form, 'timeSegment', null)
    },
  }
};
</script>

<style lang="scss" scoped></style>
